/* ´ÙÀ½ ÇÁ·Î±×·¥À» ½ÇÇàÇØ º¸°í Ãâ·ÂµÈ °á°ú¸¦ °ËÅäÇØ º¸¾Æ¶ó. coord Ŭ·¡½º¿¡ °ü·ÃµÈ +, -, =À» Áߺ¹ÇÑ´Ù. ±×¸®°í quadÀÇ ±âº» Ŭ·¡½º·Î coord¸¦ »ç¿ëÇÑ´Ù. */ #include class coord { public: int x, y; // ÁÂÇ¥ °ª coord() { x = 0, y = 0; } coord(int i, int j) { x = i, y = j; } void get_xy(int &i, int &j) { i = x, j = y; } coord operator+(coord ob2); coord operator-(coord ob2); coord operator=(coord ob2); }; // coord Ŭ·¡½º¿¡ °ü·ÃµÈ +¸¦ Áߺ¹ÇÑ´Ù. coord coord::operator+(coord ob2) { coord temp; cout << "Using coord operator+()\n"; temp.x = x + ob2.x; temp.y = y + ob2.y; return temp; } // coord Ŭ·¡½º¿¡ °ü·ÃµÈ -¸¦ Áߺ¹ÇÑ´Ù. coord coord::operator-(coord ob2) { coord temp; cout << "Using coord operator-()\n"; temp.x = x - ob2.x; temp.y = y - ob2.y; return temp; } // coord Ŭ·¡½º¿¡ °ü·ÃµÈ =À» Áߺ¹ÇÑ´Ù. coord coord::operator=(coord ob2) { cout << "Using coord operator=()\n"; x = ob2.x; y = ob2.y; return (*this); // ġȯµÈ °´Ã¼¸¦ ¹ÝȯÇÑ´Ù. } class quad : public coord { int quadrant; public: quad() { x = 0, y = 0, quadrant = 0; } quad(int x, int y) : coord(x, y) { if (x >= 0 && y >= 0) quadrant = 1; else if (x < 0 && y >= 0) quadrant = 2; else if (x < 0 && y < 0) quadrant = 3; else quadrant = 4; } void showq() { cout << "Point in Quadrant: " << quadrant << '\n'; } quad operator=(coord ob2); }; quad quad::operator=(coord ob2) { cout << "Using quad operator=()\n"; x = ob2.x; y = ob2.y; if (x >= 0 && y >= 0) quadrant = 1; else if (x < 0 && y >= 0) quadrant = 2; else if (x < 0 && y < 0) quadrant = 3; else quadrant = 4; return (*this); } int main() { quad o1(10, 10), o2(15, 3), o3; int x, y; o3 = o1 + o2; // µÎ °´Ã¼¸¦ ´õÇÑ´Ù. - operator+()¸¦ È£ÃâÇÑ´Ù. o3.get_xy(x, y); o3.showq(); cout << "(o1+o2) X: " << x << ", Y: " << y << "\n\n"; o3 = o1 - o2; // µÎ °´Ã¼¸¦ »«´Ù. o3.get_xy(x, y); o3.showq(); cout << "(o1-o2) X: " << x << ", Y: " << y << "\n\n"; o3 = o1; // °´Ã¼¸¦ ġȯÇÑ´Ù. o3.get_xy(x, y); o3.showq(); cout << "(o3=o1) X: " << x << ", Y: " << y << '\n'; return 0; }